var globalUserId, globalUserSession;
var globalTimerTyping = null;

function startTasks(){
	var obj =  eval('('+globalResponse+')');

	var reply = document.getElementById('reply');
	var replyTo = reply.getElementsByClassName('to')[0];
	var replyMessage = reply.getElementsByClassName('message')[0];
	var replyTree = document.getElementById('replyTree');
	var replyTreeList = replyTree.getElementsByClassName('list')[0];

	global.addElement(replyTree,'replyTree');
	global.addElement(replyTreeList,'replyTreeList');
	global.addElement('messageText');
	global.addElement('messages');
	global.addElement(reply);
	global.addElement(replyTo, 'replyTo');
	global.addElement(replyMessage, 'replyMessage');
	global.addElement(replyMessage, 'replyMessage');

	roomMembersHandler(obj['roomMembers']);
	roomDataHandler(obj['roomData']);
	
	resolveFirstStatus(obj['userStatus']);
	
	new Me(global.getObject('user', 'user_'+globalUserId));
	new Shortcut();
	new ReplyTree();
	new UserInformationBox();
}

function resolveFirstStatus(userStatus){
	var users = document.getElementById('users');

	global.addElement(users);
	var usersOnlineElement = users.getElementsByClassName('online')[0];
	var usersAwayElement = users.getElementsByClassName('away')[0];
	var usersOfflineElement = users.getElementsByClassName('offline')[0];

	global.addElement(usersOnlineElement,'usersOnline');
	global.addElement(usersAwayElement,'usersAway');
	global.addElement(usersOfflineElement,'usersOffline');

	var usersOnline = new Array();
	var usersAway = new Array();
	var usersOffline = new Array();
	
	var targetStatusArray, current, currentUserStatus;

	var room = global.getObject('room');

	var users = copyVar(room.group.members);
	var setFirstStatus;
	for(var k in users){
		var current = users[k];
	
		if(userStatus.length == 0) break;

		for(var i=0, iMax=userStatus.length; i<iMax; i++){
			currentUserStatus = userStatus[i];

			if(currentUserStatus[0] == current.id){
				setFirstStatus = currentUserStatus[1];
				if(setFirstStatus == 3) setFirstStatus = 1;
				global.getObject('user','user_'+current.id).status = setFirstStatus;

				if(currentUserStatus[1] == 1 || currentUserStatus[1] == 3) targetStatusArray = usersOnline;
				if(currentUserStatus[1] == 2) targetStatusArray = usersAway;
				
				targetStatusArray.push({name: current.name, obj: current});
			
				userStatus.splice(i,1);
				
				delete users[k];
				
				break;
			}
		}
	}

	for(var k in users) usersOffline.push({name: users[k].name, obj: users[k]});

	room.addUserList('usersOnline',usersOnline);
	room.addUserList('usersAway',usersAway);
	room.addUserList('usersOffline',usersOffline);

	room.updateUserListElement(usersOnline,usersOnlineElement,false);
	room.updateUserListElement(usersAway,usersAwayElement,false);
	room.updateUserListElement(usersOffline,usersOfflineElement,false);

	//setUsersByStatus(usersOnline,usersOnline);
	//setUsersByStatus(usersAway,usersAway);
	//setUsersByStatus(usersOffline,usersOffline);
}
/*
function setUsersByStatus(list,array){
	array = sort(array,'name');
	var currentUser;
	var fragment = document.createDocumentFragment();
	
	for(var i=0, iMax=array.length;i<iMax;i++){
		currentUser = array[i]['obj'];
		fragment.appendChild(currentUser.createRoomMemberItem());
	}

	list.appendChild(fragment);
}*/

function roomMembersHandler(roomMembers){
	var fragment = document.createDocumentFragment();

	var lastActivity = getDate();

	for(var i=0, iMax=roomMembers.length; i<iMax; i++){
		var current = roomMembers[i];
		
		var user = new User(current['id']);
		user.assignProperties(current['name'], current['administrator'], current['status']);
		user.setLastActivity(lastActivity);
		if('isMe' in current){
			globalUserId = current['id'];
			globalUserSession = current['session'];
		} 

		//fragment.appendChild(user.createRoomMemberItem());
	}

	//document.getElementById('users').appendChild(fragment);
}

function roomDataHandler(roomData){
	var groupData = roomData['group'];
	var group = new Group(groupData['id']);
	group.assignProperties(groupData['name']);
	group.setMembers(global.getObject('user'));

	var room = new Room(roomData['id']);
	room.set(group);
}

function performKeyPressing(event){
	//check whether it's typing or not
	clearTimeout(globalTimerTyping);
	if(globalTimerTyping == null) startTyping();
	globalTimerTyping = setTimeout(stopTyping, 2000);
	//check if a shortcut was pressed
	return global.getObject('shortcut').performKeyPressing(getKeyCode(event));
}

function performKeyReleasing(event){
	global.getObject('shortcut').performKeyReleasing(getKeyCode(event));
}